using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using HuiLian.Common.Input;
using HuiLian.Common.Output;
using HuiLian.Model.Platform;
using HuiLian.Service.Platform.Leakpoint;
using HuiLian.Service.Platform.Leakpoint.Input;
using Microsoft.AspNetCore.Http;
using HuiLian.Web.Attributes;
using HuiLian.Common.Configs;
using HuiLian.Common.Helpers;
using HuiLian.Common.Auth;
using System.Data;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System;

namespace HuiLian.Web.Controllers.Platform
{
    /// <summary>
    /// 漏水点记录管理
    /// </summary>
    public class LeakpointController : AreaController
    {
        private readonly IUser _user;
        private readonly UploadConfig _uploadConfig;
        private readonly UploadHelper _uploadHelper;
        private readonly ExcelHelper _excelHelper;
        private readonly ILeakpointService _leakpointServices;

        public LeakpointController(
            IUser user,
            IOptionsMonitor<UploadConfig> uploadConfig,
            UploadHelper uploadHelper,
            ExcelHelper excelHelper,
            ILeakpointService leakpointServices)
        {
            _user = user;
            _uploadConfig = uploadConfig.CurrentValue;
            _uploadHelper = uploadHelper;
            _excelHelper = excelHelper;
            _leakpointServices = leakpointServices;
        }

        /// <summary>
        /// 查询单条漏水点记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IResponseOutput> Get(long id)
        {
            return await _leakpointServices.GetAsync(id);
        }

        /// <summary>
        /// 查询分页漏水点记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IResponseOutput> GetPage(PageInput<LeakpointEntity> model)
        {
            return await _leakpointServices.PageAsync(model);
        }

        /// <summary>
        /// 新增漏水点记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IResponseOutput> Add(LeakpointAddInput input)
        {
            return await _leakpointServices.AddAsync(input);
        }

        /// <summary>
        /// 修改漏水点记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IResponseOutput> Update(LeakpointUpdateInput input)
        {
            return await _leakpointServices.UpdateAsync(input);
        }

        /// <summary>
        /// 删除漏水点记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IResponseOutput> SoftDelete(long id)
        {
            return await _leakpointServices.SoftDeleteAsync(id);
        }

        /// <summary>
        /// 批量删除漏水点记录
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IResponseOutput> BatchSoftDelete(long[] ids)
        {
            return await _leakpointServices.BatchSoftDeleteAsync(ids);
        }

        /// <summary>
        /// 数据导入
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [Login]
        public async Task<IResponseOutput> ExcelImport(IFormFile file)
        {
            try
            {
                var config = _uploadConfig.Document;
                var res = await _uploadHelper.UploadAsync(file, config, new { _user.Id });
                if (res.Success)
                {
                    _excelHelper.FilePath = @$"{config.UploadPath}\{res.Data.FileRelativePath}";
                    _excelHelper.ToDataTable();
                    DataTable dt = _excelHelper._dataTable;
                    var list = ExtendHelper.GetList<LeakpointEntity>(dt);

                    await _leakpointServices.AddListAsync(list);//增量方式
                    //await _leakpointServices.UpdateCount();//更新管线的破损次数//2024-11-26因为超时，所以注释
                    return ResponseOutput.Result(true);
                }

                return ResponseOutput.NotOk("上传失败！");
            }
            catch(Exception ex) { return ResponseOutput.NotOk(ex.Message); }
        }

        /// <summary>
        /// 管网绑定
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IResponseOutput> PipeBind(PipeBindInput input)
        {
            return await _leakpointServices.PipeBind(input);
        }

        /// <summary>
        /// 数据导入
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [Login]
        public async Task<IResponseOutput> GuanLianPipe(IFormFile file)
        {
            try
            {
                var config = _uploadConfig.Document;
                var res = await _uploadHelper.UploadAsync(file, config, new { _user.Id });
                if (res.Success)
                {
                    _excelHelper.FilePath = @$"{config.UploadPath}\{res.Data.FileRelativePath}";
                    _excelHelper.ToDataTable();
                    DataTable dt = _excelHelper._dataTable;
                    var list = ExtendHelper.GetList<LeakpointEntity>(dt);

                    return await _leakpointServices.GuanLianPipeAsync(list);
                }

                return ResponseOutput.NotOk("上传失败！");
            }
            catch (Exception ex) { return ResponseOutput.NotOk(ex.Message); }
        }

        /// <summary>
        /// 更新管线的破损次数
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IResponseOutput> UpdateCount()
        {
            return await _leakpointServices.UpdateCount();
        }
    }
}
